package com.ruoyi.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis核心类
 * @author dell
 *
 */
@Component
public class RedisService {

	@Autowired
	@Qualifier("redisTemplate")
	RedisTemplate redisTemp;
	
	/**
	 * 从队列中取出一条信息
	 * @param queueName
	 */
	public String brpopQueueVal(String queueName)
	{
		return (String) redisTemp.opsForList().rightPop(queueName);
	}
	/**
	 * 从队列中取出一条信息
	 * @param queueName
	 */
	public String brpopQueueValBlock(String queueName)
	{
		redisTemp.opsForStream().read();
		return (String) redisTemp.opsForList().rightPop(queueName, 2, TimeUnit.MINUTES);
	}
	/**
	 * 像队列中插入一条信息
	 * @param queueName
	 * @param data
	 */
	public void pushQueueVal(String queueName,String data)
	{
		redisTemp.opsForList().leftPush(queueName, data);
	}
	/**
	 * 删除值
	 * @param key
	 */
	public void delVal(String key)
	{
		redisTemp.delete(key);
	}
	/**
	 * 批量删除值
	 * @param selkey
	 */
	public void delValBatch(String selkey)
	{
		Set<String> keySet = redisTemp.keys(selkey);
		if(keySet!=null){
			for(String key : keySet){
				redisTemp.delete(key);
			}
		}
	}
	/**
	 * 像redis插值
	 * @param key
	 * @param value
	 */
	public void setVal(String key,String value)
	{
		ValueOperations<String, Object> vo= redisTemp.opsForValue();
		vo.set(key, value);
	}
	/**
	 * 像redis插值
	 * @param key
	 * @param obj
	 */
	public void setVal(String key,Object obj)
	{
		ValueOperations<String, Object> vo= redisTemp.opsForValue();
		vo.set(key, obj);
	}
	/**
	 * 获取值
	 * @param key
	 */
	public Object getVal(String key)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		return vo.get(key);
	}
	/**
	 * 获取值
	 * @param key
	 */
	public String getStrVal(String key)
	{
		String res=null;
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		Object obj=vo.get(key);
		if(obj!=null)res=obj.toString();
		return res;
	}
	/**
	 * 像redis插值，time时间后过期，时间为分
	 * @param key
	 * @param value
	 * @param time
	 */
	public void setVal(String key,String value,int time)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		vo.set(key, value);
		redisTemp.expire(key, time, TimeUnit.MINUTES);//设置过期时间
	}
	/**
	 * 像redis插值，time时间后过期，时间为分
	 * @param key
	 * @param obj
	 * @param time
	 */
	public void setVal(String key,Object obj,int time)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		vo.set(key, obj);
		redisTemp.expire(key, time, TimeUnit.MINUTES);//设置过期时间
	}
	/**
	 * 像redis插值，time时间后过期，时间为分
	 * @param key
	 * @param value
	 * @param time
	 */
	public void setVal_M(String key,String value,int time)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		vo.set(key, value);
		redisTemp.expire(key, time, TimeUnit.MINUTES);//设置过期时间
	}
	/**
	 * 像redis插值，time时间后过期，时间为分
	 * @param key
	 * @param obj
	 * @param time
	 */
	public void setVal_M(String key,Object obj,int time)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		vo.set(key, obj);
		redisTemp.expire(key, time, TimeUnit.MINUTES);//设置过期时间
	}
	/**
	 * 像redis插值，time时间后过期，时间为秒
	 * @param key
	 * @param value
	 * @param time
	 */
	public void setVal_S(String key,String value,int time)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		vo.set(key, value);
		redisTemp.expire(key, time, TimeUnit.SECONDS);//设置过期时间
	}
	/**
	 * 像redis插值，time时间后过期，时间为秒
	 * @param key
	 * @param obj
	 * @param time
	 */
	public void setVal_S(String key,Object obj,int time)
	{
		ValueOperations<String, Object> vo=redisTemp.opsForValue();
		vo.set(key, obj);
		redisTemp.expire(key, time, TimeUnit.SECONDS);//设置过期时间
	}

	/**
	 * 从HASH中获取数据
	 * @param setName
	 * @param key
	 * @return
	 */
	public String getHashVal(String setName ,String key)
	{
		HashOperations<String, String, String> ho = redisTemp.opsForHash();
		return ho.get(setName,key);

	}

	/**
	 * 像HASH中添加key_val
	 * @param setName
	 * @param key
	 * @param val
	 */
	public void setHashVal(String setName ,String key,String val)
	{
		HashOperations<String, String, String> ho = redisTemp.opsForHash();
		ho.put(setName,key,val);

	}
	/**
	 * 删除HASH中key
	 * @param setName
	 * @param key
	 */
	public void delHashVal(String setName ,String... key)
	{
		HashOperations<String, String, String> ho = redisTemp.opsForHash();
		ho.delete(setName,key);
	}
	
	/**
	 * 像set中插入数据
	 * @param setName
	 * @param val
	 */
	public void insSetVal(String setName,Object val){
		SetOperations<String, Object> so = redisTemp.opsForSet();
		so.add(setName, val);
	}
	/**
	 * 从set中删除数据
	 * @param setName
	 * @param val
	 */
	public void delSetVal(String setName,Object val){
		SetOperations<String, Object> so = redisTemp.opsForSet();
		if(so.isMember(setName, val)){
			so.remove(setName, val);
		}
	}
	
	/**
	 * 从set中拉去全部值
	 * @param setName
	 * @return
	 */
	public Set<String> getSet(String setName){
		SetOperations<String, String> so = redisTemp.opsForSet();
		Set<String> res = so.members(setName);
		return res;
	}

	/**
	 * 根据关键字，查询所有key
	 * @param key
	 * @return
	 */
	public Set<String> getKeys(String key){
		return redisTemp.keys(key);
	}
}
